'use strict';



var
    form = $('#test-form'),
    langs = form.find('[name=lang]'),
    selectAll = form.find('label.selectAll :checkbox'),
    selectAllLabel = form.find('label.selectAll span.selectAll'),
    deselectAllLabel = form.find('label.selectAll span.deselectAll'),
    invertSelect = form.find('a.invertSelect');

// 重置初始化状态:
form.find('*').show().off();
form.find(':checkbox').prop('checked', false).off();
deselectAllLabel.hide();
// 拦截form提交事件:
form.off().submit(function (e) {
    e.preventDefault();
    alert(form.serialize());
});
selectAll.click(function() {
    if (selectAll.prop('checked')){
        langs.prop('checked', true);
        selectAllLabel.hide();
        deselectAllLabel.show();
    } else {
        langs.prop('checked', false);
        deselectAllLabel.hide();
        selectAllLabel.show();
    }
});

invertSelect.click(function() {
    langs.each(function() {
        if ($(this).prop('checked')) {
            $(this).prop('checked', false);
        } else {
            $(this).prop('checked', true);
        }
    });
    langs.change();
});



langs.change(function() {
    let hasCheckedCount = form.find('[name="lang"]:checked').length;

    if (hasCheckedCount === langs.length) {
        selectAll.prop('checked', true);
        selectAllLabel.hide();
        deselectAllLabel.show();
    } else {
        selectAll.prop('checked', false);
        deselectAllLabel.hide();
        selectAllLabel.show();
    }
});